package com.jiepos.mpos.biz.code.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiepos.mpos.common.constant.DBConstant.Table;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.dao.InfCnapsDao;
import com.jiepos.mpos.common.entity.App_Inf_CnapsEntity;
import com.jiepos.mpos.core.entity.Pagination;
import com.jiepos.mpos.core.service.BaseService;
import com.jiepos.mpos.core.util.CollectionUtil;
import com.jiepos.mpos.core.util.IDFactoryUtil;
import com.jiepos.mpos.core.util.StringUtils;

/**
 * 
 * 
 * <p>标题: —— 联行号表服务类</p>
 * <p>模块: 通用平台</p>
 * <p>版权: Copyright (c) 2017 江苏杰博实信息技术有限公司
 * @author 张学涛
 * @version 1.0
 * @since 2017年9月14日下午2:44:24
 * @history（历次修订内容、修订人、修订时间等）
 */
@Service
public class InfCnapsService extends BaseService {
	
	@Autowired
	private InfCnapsDao infCnapsDao;
	
	/**
	 * 查询联行号列表json分页数据
	 * @param pagination
	 * @param appInfCnapsEntity
	 * @return
	 */
	public Map getAppInfCnapsList(Pagination pagination,App_Inf_CnapsEntity appInfCnapsEntity){
		StringBuffer sb = getInitSql("t1").append(",");
		sb.append(" t1.cnaps_uuid as uuid,t1.cnapsno,t1.bank_nm,t1.bank_addr,t1.super_bank_no");
		sb.append(" from ");
		sb.append(Table.T_APP_INF_CNAPS);
		sb.append(" t1 where 1=1 ");
		List list = new ArrayList();
		if (!StringUtils.isBlank(appInfCnapsEntity.getCnapsno())) {
			sb.append( " and t1.cnapsno like ? " );
			list.add("%" + appInfCnapsEntity.getCnapsno() + "%");
		}
		if (!StringUtils.isBlank(appInfCnapsEntity.getBank_nm())) {
			sb.append( " and t1.bank_nm like ? " );
			list.add("%" + appInfCnapsEntity.getBank_nm() + "%");
		}
		if (!StringUtils.isBlank(appInfCnapsEntity.getBank_addr())) {
			sb.append( " and t1.bank_addr like ? " );
			list.add("%" + appInfCnapsEntity.getBank_addr() + "%");
		}
		pagination.queryString = sb.toString();
		pagination = this.getOrderSql(pagination, "t1");
		return infCnapsDao.getPaginationMap(pagination, "t1", list);
	}
	
	/**
	 * 保存联行号信息
	 * @param appInfCnapsEntity
	 * @param map
	 */
	public void saveCnaps(App_Inf_CnapsEntity appInfCnapsEntity,Map map) {
		if (!StringUtils.isBlank(appInfCnapsEntity.getCnaps_uuid())) {
			App_Inf_CnapsEntity newAppInfCnapsEntity=infCnapsDao.findByParam("cnaps_uuid", appInfCnapsEntity.getCnaps_uuid());
			newAppInfCnapsEntity.setRec_upd_usr_id(String.valueOf(map.get(SystemConstant.SESSION_LOGIN_USER)));
			newAppInfCnapsEntity.setRec_upd_ts(new Timestamp(System.currentTimeMillis()));
			newAppInfCnapsEntity.setLast_oper_in(SystemConstant.OPER_UPDATE);
			newAppInfCnapsEntity.setCnapsno(appInfCnapsEntity.getCnapsno());
			newAppInfCnapsEntity.setBank_nm(appInfCnapsEntity.getBank_nm());
			newAppInfCnapsEntity.setBank_addr(appInfCnapsEntity.getBank_addr());
			newAppInfCnapsEntity.setSuper_bank_no(appInfCnapsEntity.getSuper_bank_no());
			List noChangeList = new ArrayList();
			noChangeList.add("cnaps_uuid");
			noChangeList.add("rec_crt_ts");
			infCnapsDao.update(newAppInfCnapsEntity, noChangeList);
		}else{
			appInfCnapsEntity.setCnaps_uuid(IDFactoryUtil.getUUID());
			appInfCnapsEntity.setRec_upd_usr_id(String.valueOf(map.get(SystemConstant.SESSION_LOGIN_USER)));
			appInfCnapsEntity.setRec_crt_ts(new Timestamp(System.currentTimeMillis()));
			appInfCnapsEntity.setRec_upd_ts(new Timestamp(System.currentTimeMillis()));
			appInfCnapsEntity.setRec_st(SystemConstant.NORMAL_STATUS);
			appInfCnapsEntity.setLast_oper_in(SystemConstant.OPER_ADD);
			infCnapsDao.save(appInfCnapsEntity);
		}
	}
	
	/**
	 * 根据记录id查询联行号信息实体对象
	 * @param code_uuid
	 * @return
	 */
	public App_Inf_CnapsEntity getCnapsById(String cnaps_uuid) {
		if (StringUtils.isBlank(cnaps_uuid) || cnaps_uuid.equals("undefined")) {
			return new App_Inf_CnapsEntity();
		}
		return infCnapsDao.findByParam("cnaps_uuid", cnaps_uuid);
	}
	
	/**
	 * 根据联行号查询实体对象
	 * @param cnapsNo
	 * @return
	 */
	public App_Inf_CnapsEntity getCnapsByNo(String cnapsNo) {
		if (StringUtils.isBlank(cnapsNo)) {
			return new App_Inf_CnapsEntity();
		}
		return infCnapsDao.findByParam("cnapsno", cnapsNo);
	}
	
	/**
	 * 根据记录id集合删除数据
	 * @param code_uuids
	 */
	public void deleteCnapsByIds(String cnaps_uuids) {
		infCnapsDao.deleteByStatus("cnaps_uuid",cnaps_uuids);
	}
	
	/**
	 * 校验联行号
	 * @param appInfCnapsEntity
	 * @return
	 */
	public String checkCnaps(App_Inf_CnapsEntity appInfCnapsEntity){
		StringBuffer sb = getInitSql("t1").append(",");
		sb.append("t1.cnaps_uuid as uuid,t1.cnapsno,t1.bank_nm,t1.bank_addr");
		sb.append(" from ");
		sb.append(Table.T_APP_INF_CNAPS);
		sb.append(" t1 where 1=1 ");
		List list = new ArrayList();
		if (!StringUtils.isBlank(appInfCnapsEntity.getCnapsno())) {
			sb.append( " and t1.cnapsno = ? " );
			list.add(appInfCnapsEntity.getCnapsno());
		}				
		if (!StringUtils.isBlank(appInfCnapsEntity.getCnaps_uuid())) {
			sb.append( " and t1.cnaps_uuid != ? " );
			list.add(appInfCnapsEntity.getCnaps_uuid());
		}
		List result=infCnapsDao.queryForList(sb.toString(), list);
		if(result.size()==0||result==null){
			return SystemConstant.SUCCESS;
		}
		return SystemConstant.ERROR;
	}
	
	/**
	 * 获取联行号
	 * @param custom_no
	 * @return
	 */
	public List getCnapsInfoByCondition(String area_cd,String super_bank_no){
		StringBuffer sqlBuf = new StringBuffer();
		List<String> list = new ArrayList<>();
		String str = "";
		sqlBuf.append(" SELECT DISTINCT tt.BANK_AREA_CD FROM ").append(Table.T_APP_AREA_CD).append(" tt ")
			.append("  START WITH tt.AREA_CD = ? CONNECT BY PRIOR tt.AREA_CD = tt.SUP_AREA_CD ");
		list.add(area_cd);
		List<Map<String,String>> areaList = infCnapsDao.queryForList(sqlBuf.toString(), list);
		if(!CollectionUtil.isEmpty(areaList) && !StringUtils.isBlank(super_bank_no)){
			StringBuffer sqlBuf2 = new StringBuffer();
			sqlBuf2.append("select cnapsno as \"cnapsno\",bank_nm as \"bank_nm\",bank_addr as \"bank_addr\" from ")
				.append(Table.T_APP_INF_CNAPS).append("  where rec_st != '0'  and (cnapsno like '");
			
			for(int i=0;i<areaList.size();i++){
				str = super_bank_no.substring(0, 3)+areaList.get(i).get("bank_area_cd");
				if(i<areaList.size()-1){
					sqlBuf2.append(str+"%' or cnapsno like '");
					continue;
				}
				if(i == areaList.size() -1){
					sqlBuf2.append(str +"%' )");
				}
			}
				List<Map<String, Object>> result = infCnapsDao.queryForList(sqlBuf2.toString(), null);
				return result;
		}
		return null;
	}

}
